<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>
    <a-typography-paragraph v-if="spaceId" type="secondary">
      保存至空间：<a :href="`/space/${spaceId}`" target="_blank">{{ spaceId }}</a>
    </a-typography-paragraph>

    <!-- 选择上传方式 -->
    <a-tabs v-model:activeKey="uploadType"
      >>
      <a-tab-pane key="file" tab="文件上传">
        <PictureUpload :picture="picture" :onSuccess="onSuccess" :spaceId="spaceId" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL 上传" force-render>
        <UrlPictureUpload :picture="picture" :onSuccess="onSuccess"  :spaceId="spaceId"/>
      </a-tab-pane>
    </a-tabs>

    <a-form
      v-if="picture.id"
      :model="pictureFrom"
      name="basic"
      autocomplete="off"
      @finish="handleSubmit"
      layout="vertical"
    >
      <a-form-item label="名称" name="name">
        <a-input v-model:value="pictureFrom.name" placeholder="请输入名称" />
      </a-form-item>

      <a-form-item label="简介" name="introduction">
        <a-input v-model:value="pictureFrom.introduction" placeholder="请输入简介" />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="pictureFrom.category"
          :options="categoryOptions"
          placeholder="请输入分类"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="pictureFrom.tags"
          :options="tagOptions"
          mode="tags"
          placeholder="请输入标签"
          allowClear
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">{{
          route.query?.id ? '修改' : '创建'
        }}</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import PictureUpload from '@/components/PictureUpload.vue'
import UrlPictureUpload from '@/components/UrlPictureUpload.vue'
import { ref, reactive, onMounted, onBeforeMount } from 'vue'
import { message } from 'ant-design-vue'
import {
  listPictureTagCategoryUsingGet,
  editPictureUsingPost,
  getPictureVoByIdUsingGet,
} from '@/api/tupian'
import { useRouter, useRoute } from 'vue-router'
const picture = ref<API.PictureVO>({})
const route = useRoute()
const uploadType = ref<'file' | 'url'>('file')
const spaceId = ref<string>()

const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureFrom.name = newPicture.name
}

const pictureFrom = reactive<API.PictureEditRequest>({})

const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])
const router = useRouter()
// 提交表单
const handleSubmit = async () => {
  if (!picture.value.id) {
    message.error('请先上传图片')
    return
  }
  const res = await editPictureUsingPost({ id: picture.value.id, ...pictureFrom })
  if (res.data.code === 0 && res.data.data) {
    if (route.query.id) {
      message.success('图片修改成功')
    } else {
      message.success('图片创建成功')
    }
    router.push({ path: `/picture/${picture.value.id}` })
  } else {
    message.error('图片创建失败，' + res.data.message)
  }
}
const getCategoryTagsOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('获取分类标签失败，' + res.data.message)
  }
}
onBeforeMount(() => {
  if (route.query.spaceId) {
    spaceId.value = route.query.spaceId as string
  }
  if (route.query.id) {
    getPictureById(route.query.id as string)
  }
})
onMounted(() => {
  getCategoryTagsOptions()
})
const getPictureById = async (id: string) => {
  const res = await getPictureVoByIdUsingGet({ id })

  if (res.data.code === 0 && res.data.data) {
    picture.value = res.data.data
    pictureFrom.name = res.data.data.name
    pictureFrom.introduction = res.data.data.introduction
    pictureFrom.category = res.data.data.category
    pictureFrom.tags = res.data.data.tags
  } else {
    message.error('获取图片失败，' + res.data.message)
  }
}
</script>

<style scoped>
#addPicturePage {
  width: 80%;
  margin: 0 auto;
}
</style>
